﻿using System;
using Microsoft.Win32;

namespace MediaAssistant.Helper
{
    public class RegistryHelper
    {
        private const string LastAddedFolderLocationKey = "LastAddedFolderLocation";
        private const string LastSelectedPlaylistKey = "LastSelectedPlaylist";
        private const string VolumeKey = "Volume";
        private const string IsMutedKey = "IsMuted";
        private const string ShowDrivesMoviesOnlyKey = "ShowDrivesMoviesOnlyKey";
        private const string SelectedMusicDJKey = "SelectedMusicDJ";
        private const string SubKey = "SOFTWARE\\Media Assistant";
        private const string ProfileNameKey = "ProfileName";

        public static string LastAddedFolderLocation
        {
            get { return GetStringValue(LastAddedFolderLocationKey); }
            set
            {
                SetStringValue(LastAddedFolderLocationKey, value);
            }
        }

        public static double Volume
        {
            get
            {
                var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
                if (subKey == null)
                {
                    return 0.5;
                }
                try
                {
                    var value = subKey.GetValue(VolumeKey);
                    if(value!=null)
                        return double.Parse(value.ToString());
                }
                catch
                {
                    
                }
                return 0.5;
            }
            set
            {
                try
                {
                    var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                    if (subKey != null)
                        subKey.SetValue(VolumeKey, value);
                }
                catch (Exception)
                {
                }
            }
        }

        public static string SelectedMusicDJ
        {
            get
            {
                return GetStringValue(SelectedMusicDJKey);
            }
            set
            {
                SetStringValue(SelectedMusicDJKey, value);
            }
        }

        private static void SetStringValue(string key, string value)
        {
            try
            {
                var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                if (subKey != null)
                    subKey.SetValue(key, value);
            }
            catch (Exception)
            {
            }
        }

        private static string GetStringValue(string key)
        {
            var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
            if (subKey == null)
            {
                return null;
            }
            try
            {
                return (string)subKey.GetValue(key);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool IsMuted
        {
            get
            {
                var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
                if (subKey == null)
                {
                    return false;
                }
                try
                {
                    var value = subKey.GetValue(IsMutedKey);
                    if(value!=null)
                        return bool.Parse(value.ToString());
                }
                catch
                {
                    
                }
                return false;
            }
            set
            {
                try
                {
                    var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                    if (subKey != null)
                        subKey.SetValue(IsMutedKey, value);
                }
                catch (Exception)
                {
                }
            }
        }
        public static bool ShowDrivesMoviesOnly
        {
            get
            {
                return GetBoolValue(ShowDrivesMoviesOnlyKey, false);
            }
            set 
            {
                SetBoolValue(value, ShowDrivesMoviesOnlyKey);
            }
        }

        private static void SetBoolValue(bool value, string key)
        {
            try
            {
                var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                if (subKey != null)
                    subKey.SetValue(key, value);
            }
            catch (Exception)
            {
            }
        }

        private static bool GetBoolValue(string key, bool defaultValue)
        {
            var value = defaultValue;
            var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
            if (subKey != null)
            {
                try
                {
                    var subKeyValue = subKey.GetValue(key);
                    if(subKeyValue!=null)
                        value = bool.Parse(subKeyValue.ToString());
                }
                catch
                {
                }
            }
            return value;
        }


        public static string ProfileName
        {
            get { return GetStringValue(ProfileNameKey); }
            set { SetStringValue(ProfileNameKey,value); }
        }

        public static string LastSelectedPlaylist
        {
            get { return GetStringValue(LastSelectedPlaylistKey); }
            set { SetStringValue(LastSelectedPlaylistKey, value); }
        }

        public static int ArtistPreferance
        {
            get
            {
                var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
                if (subKey == null)
                {
                    return 5;
                }
                try
                {
                    return int.Parse(subKey.GetValue("Artist Preferance").ToString());
                }
                catch (Exception)
                {
                    return 5;
                }
            }
            set
            {
                try
                {
                    var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                    if (subKey != null)
                        subKey.SetValue("Artist Preferance", value);
                }
                catch (Exception)
                {
                }
            }
        }

        public static int AlbumPreferance
        {
            get
            {
                var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
                if (subKey == null)
                {
                    return 5;
                }
                try
                {
                    return int.Parse(subKey.GetValue("Album Preferance").ToString());
                }
                catch (Exception)
                {
                    return 5;
                }
            }
            set
            {
                try
                {
                    var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                    if (subKey != null)
                        subKey.SetValue("Album Preferance", value);
                }
                catch (Exception)
                {
                }
            }
        }

        public static int ComposerPreferance
        {
            get
            {
                var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
                if (subKey == null)
                {
                    return 5;
                }
                try
                {
                    return int.Parse(subKey.GetValue("Composer Preferance").ToString());
                }
                catch (Exception)
                {
                    return 5;
                }
            }
            set
            {
                try
                {
                    var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                    if (subKey != null)
                        subKey.SetValue("Composer Preferance", value);
                }
                catch (Exception)
                {
                }
            }
        }

        public static int GenrePreferance
        {
            get
            {
                var subKey = Registry.CurrentUser.OpenSubKey(SubKey);
                if (subKey == null)
                {
                    GenrePreferance = 5;
                    return 5;
                }
                try
                {
                    return int.Parse(subKey.GetValue("Genre Preferance").ToString());
                }
                catch (Exception)
                {
                    GenrePreferance = 5;
                    return 5;
                }
            }
            set
            {
                try
                {
                    var subKey = Registry.CurrentUser.CreateSubKey(SubKey);
                    if (subKey != null)
                        subKey.SetValue("Genre Preferance", value);
                }
                catch (Exception)
                {
                }
            }
        }

        public static string FromEmailAddress
        {
            get { return GetStringValue("FromEmailAddress"); }
            set { SetStringValue("FromEmailAddress", value??string.Empty); }
        }

        public static string LastResultType
        {
            get
            {
                var stringValue = GetStringValue("LastResultType");
                return string.IsNullOrWhiteSpace(stringValue) ? "Music" : stringValue;
            }
            set { SetStringValue("LastResultType", value); }
        }

        public static string ScanOption
        {
            get { return GetStringValue("ScanOption"); }
            set { SetStringValue("ScanOption", value ?? string.Empty); }
        }

        public static bool CanSearchMovie
        {
            get
            {
                return GetBoolValue("CanSearchMovie", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchMovie");
            }
        }

        public static bool CanSearchDirectors
        {
            get
            {
                return GetBoolValue("CanSearchDirectors", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchDirectors");
            }
        }

        public static bool CanSearchWriters
        {
            get
            {
                return GetBoolValue("CanSearchWriters", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchWriters");
            }
        }

        public static bool CanSearchActors
        {
            get
            {
                return GetBoolValue("CanSearchActors", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchActors");
            }
        }

        public static bool CanSearchGenres
        {
            get
            {
                return GetBoolValue("CanSearchGenres", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchGenres");
            }
        }

        public static bool CanSearchMusic
        {
            get
            {
                return GetBoolValue("CanSearchMusic", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchMusic");
            }
        }

        public static bool CanSearchAlbum
        {
            get
            {
                return GetBoolValue("CanSearchAlbum", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchAlbum");
            }
        }

        public static bool CanSearchArtist
        {
            get
            {
                return GetBoolValue("CanSearchArtist", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchArtist");
            }
        }

        public static bool CanSearchComposer
        {
            get
            {
                return GetBoolValue("CanSearchComposer", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchComposer");
            }
        }

        public static bool CanSearchGenre
        {
            get
            {
                return GetBoolValue("CanSearchGenre", true);
            }
            set
            {
                SetBoolValue(value, "CanSearchGenre");
            }
        }
    }
}
